﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class UIAutoGenWin : EditorWindow {
    [MenuItem("UI管理/创建UI界面脚本")]

    static void run()
    {
        Debug.Log("创建UI脚本");
        EditorWindow.GetWindow<UIAutoGenWin>();
    }

    private void OnGUI()
    {
        GUILayout.Label("选择一个UI视图根节点");
        if (GUILayout.Button("生成代码"))
        {
            Debug.Log("开始生成");
            if (Selection.activeGameObject != null)
            {
                CreatUISourceUtil.CreatUISourceFile(Selection.activeGameObject);
                AssetDatabase.Refresh();//刷新数据库，不然生成以后unity工程中不显示脚本
            }

            Debug.Log("生成结束");
        }
        //选择以后不会刷新，点击以后才会刷新
        if (Selection.activeGameObject != null)
        {
            GUILayout.Label(Selection.activeGameObject.name);
        }
        else
        {
            GUILayout.Label("没有选中UI节点，无法生成");
        }
    }


    /// <summary>
    /// 重载选择变化函数
    /// 强制刷新界面
    /// </summary>
    private void OnSelectionChange()
    {
        this.Repaint();
    }
}
